var app = {}

function httpSetApp(appdata) {
    console.log(appdata)
    app.globalData = appdata
}

function httpRequest(url, method, data, needsk, jsonrequest) {
    console.log("请求参数",data);
    return new Promise((resolve, reject) => {
        let header = needsk ? { sessionkey: wx.getStorageSync('sessionKey') } : {}
        if (needsk && header.sessionkey == null) {
            reject("请先登录")
                // wx.reLaunch({
                //   url: '/pages/index/index?tab=mine'
                // })
        } else {
             if (method.toUpperCase() !== "GET" && !jsonrequest) { //使用form表单提交
            // if (method !== "GET" && !jsonrequest) {
                header['Content-Type'] = 'application/json;charset=UTF-8'
            }
            // console.log('header', header)
            wx.request({
                header: header,
                url: app.globalData.basicUrl + url,
                // url:'http://192.168.28.235:8080/system'+url,
                data,
                method,
                success: (res => {
                    console.log(url, res);
                    if (res.statusCode === 200) {
                        if (res.data.code == "not-login") {
                            reject("请先登录")
                                // wx.reLaunch({
                                //   url: '/pages/index/index?tab=mine'
                                // })
                    }else if(res.data.code != 200){
                        resolve(res)
                    }
                        resolve(res)
                    } else {
                        reject(res)
                    }
                }),
                fail: (res => {
                    reject(res)
                })
            })
        }
    })

}

// export default httpRequest
export { httpRequest, httpSetApp }